package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.YouhuiBikeLvAdapter;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.SettlementBikeDetailBean;
import com.ekuaitu.kuaitu.map.GeocodeSearchViewUtils;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.TimeUtils;
import com.ekuaitu.kuaitu.widget.MyListView;
import com.umeng.analytics.MobclickAgent;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class BikeSettlementDetailActivity extends BaseActivity {

    @BindView(R.id.order_bike_return)
    ImageView orderBikeReturn;
    @BindView(R.id.order_toolbar_bike)
    RelativeLayout orderToolbarBike;
    @BindView(R.id.order_bike_id)
    TextView orderBikeId;
    @BindView(R.id.order_bike_bikeid)
    TextView orderBikeBikeid;
    @BindView(R.id.order_bike_time0)
    TextView orderBikeTime0;
    @BindView(R.id.order_bike_tv_haoshi)
    TextView orderBikeTvHaoshi;
    @BindView(R.id.order_bike_orderTime)
    TextView orderBikeOrderTime;
    @BindView(R.id.order_bike_startTime)
    TextView orderBikeStartTime;
    @BindView(R.id.order_bike_startLocation)
    TextView orderBikeStartLocation;
    @BindView(R.id.order_bike_endTime)
    TextView orderBikeEndTime;
    @BindView(R.id.order_bike_endLocation)
    TextView orderBikeEndLocation;
    @BindView(R.id.order_bike_rl_haoshi)
    LinearLayout orderBikeRlHaoshi;
    @BindView(R.id.order_bike_money)
    TextView orderBikeMoney;
    @BindView(R.id.order_bike_xcdj)
    TextView orderBikeXcdj;
    @BindView(R.id.order_bike_xcmoney)
    TextView orderBikeXcmoney;
    @BindView(R.id.order_bike_coupon)
    TextView orderBikeCoupon;
    @BindView(R.id.order_bike_tv_coupon)
    TextView orderBikeTvCoupon;
    @BindView(R.id.order_bike_coupon_rl)
    RelativeLayout orderBikeCouponRl;
    @BindView(R.id.order_rule)
    TextView orderRule;
    @BindView(R.id.order_bike_sv)
    ScrollView orderBikeSv;
    @BindView(R.id.order_bike_payMoney)
    TextView orderBikePayMoney;
    @BindView(R.id.progressBar_bike_settlement)
    RelativeLayout progressBarBikeSettlement;
    @BindView(R.id.order_bike_ll_haoshi)
    RelativeLayout orderBikeLlHaoshi;
    @BindView(R.id.order_bike_yhmoney)
    TextView orderBikeYhmoney;
    @BindView(R.id.order_bike_yh_rl)
    RelativeLayout orderBikeYhRl;
    @BindView(R.id.order_bike_yh_line)
    View orderBikeYhLine;
    @BindView(R.id.order_bike_yh_lv)
    MyListView orderBikeYhLv;
    @BindView(R.id.order_bike_yh_block)
    View orderBikeYhBlock;
    @BindView(R.id.order_bike_coupon_block)
    View orderBikeCouponBlock;
    @BindView(R.id.bike_settlement_orderpay)
    TextView tvBottomOrderPay;
    @BindView(R.id.bike_settlement_yhpay)
    TextView tvBottomYhPay;
    @BindView(R.id.order_bike_movepay_layout)
    RelativeLayout rlMovePayLayout;
    @BindView(R.id.order_bike_movepaymoney)
    TextView tvMovepayMoney;
    private Context mContext = this;
    private String orderId;
    private String bikeId;
    private String couponId = "0";
    private double couponPrice = 0;
    private double payMoney;
    private String paymentMethod = "1";
    private double resultMoney;
    private int flag = 0;
    private int flag1 = 0;
    private int couponPay;
    private int couponNum = 0;
    private String reason;
    private String couponTitle;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bike_settlement_detail;
    }

    @Override
    public void initView() {
        MobclickAgent.onEvent(this, Constants.PAYVCAPPEARE);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
        Intent intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        bikeId = intent.getStringExtra(IntentKey.INTENT_BIKE_ID);
        getSettlementBikeDetail();
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        couponPrice = intent.getDoubleExtra(IntentKey.INTENT_COUPONPRICE, 0);
        couponTitle = intent.getStringExtra(IntentKey.INTENT_COUPONTITLE);
        if (couponPrice <= 0) {
            orderBikeTvCoupon.setText("未使用优惠券");
        } else {
            orderBikeTvCoupon.setText(couponPrice + "元" + couponTitle);
        }
    }

    private void getSettlementBikeDetail() {
        SettlementBikeDetailBean settlementDetailBean = (SettlementBikeDetailBean) getIntent().getSerializableExtra(IntentKey.INTENT_SETTLEMENT_BIKE_DETAIL);
        SettlementBikeDetailBean.AttachmentBean result = settlementDetailBean.getAttachment();
        orderBikeId.setText(result.getModel().getOrderNum());
        orderBikeBikeid.setText(result.getModel().getLicenseTag());
        orderBikeTvHaoshi.setText(TimeUtils.formatMin(result.getModel().getMinute() * 60 + ""));
        orderBikeOrderTime.setText(TimeUtils.getFormatDate(result.getModel().getOrderTime() + "", "yyyy-MM-dd HH:mm:ss"));
        orderBikeStartTime.setText(TimeUtils.getFormatDate(result.getModel().getStartBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
        orderBikeEndTime.setText(TimeUtils.getFormatDate(result.getModel().getEndBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
        GeocodeSearchViewUtils geocodeSearchViewUtils1 = new GeocodeSearchViewUtils();
        geocodeSearchViewUtils1.startGeocodeSearch(mContext, result.getModel().getOnLatitude(), result.getModel().getOnLongitude(), orderBikeStartLocation);
        GeocodeSearchViewUtils geocodeSearchViewUtils2 = new GeocodeSearchViewUtils();
        geocodeSearchViewUtils2.startGeocodeSearch(mContext, result.getModel().getDownLatitude(), result.getModel().getDownLongitude(), orderBikeEndLocation);
        DecimalFormat df = new DecimalFormat("0.00");
        //订单金额
        orderBikeMoney.setText(df.format(result.getModel().getOrderPrice()) + "元");
        orderBikeXcmoney.setText(df.format(result.getModel().getOrderPrice()) + "元");
        orderBikeXcdj.setText("行程：" + result.getModel().getPriceMsg());
        //挪车费用
        String violationmoney = result.getModel().getViolationmoney();
        boolean isViolation = false;
        if (!TextUtils.isEmpty(violationmoney)) {
            isViolation = Integer.parseInt(violationmoney) > 0;
        }
        if (isViolation) {
            rlMovePayLayout.setVisibility(View.VISIBLE);
            float money = ((float) Integer.parseInt(violationmoney)) / 100;
            tvMovepayMoney.setText(money + "元");
        } else {
            rlMovePayLayout.setVisibility(View.GONE);
        }
        //订单
        tvBottomOrderPay.setText("订单￥" + df.format(result.getModel().getOrderPrice()) + "元");

        payMoney = result.getModel().getRealPrice();
        resultMoney = payMoney;
        //优惠费用
        YouhuiBikeLvAdapter adapter = new YouhuiBikeLvAdapter(result.getModel().getSpecialModelList(), mContext);
        orderBikeYhLv.setAdapter(adapter);
        double totalYh = 0;
        for (int i = 0; i < result.getModel().getSpecialModelList().size(); i++) {
            totalYh = totalYh + result.getModel().getSpecialModelList().get(i).getSubMoney();
        }
        orderBikeYhmoney.setText((totalYh + couponPrice) + "元");
        tvBottomYhPay.setText("优惠￥" + (totalYh + couponPrice) + "元");
        //最终支付金额
        double realPrice = result.getModel().getRealPrice() - couponPrice;
        orderBikePayMoney.setText(df.format(realPrice < 0 ? 0 : realPrice) + "元");
        //能不能使用优惠券
        couponPay = result.getModel().getCouponPay();
        if (result.getModel().getCouponPay() == 0) {
            couponNum = 0;
            reason = result.getModel().getCouponForbidReason();
            orderBikeCouponRl.setVisibility(View.VISIBLE);
            orderBikeCouponBlock.setVisibility(View.VISIBLE);
            orderBikeCouponRl.setClickable(false);
            orderBikeTvCoupon.setText(result.getModel().getCouponForbidReason());
        } else if (result.getModel().getCouponPay() == 1) {
            //没有优惠券，隐藏优惠券框
            if (result.getModel().getCoupons() == null || result.getModel().getCoupons().size() == 0) {
                couponNum = 0;
                orderBikeCouponRl.setVisibility(View.GONE);
                orderBikeCouponBlock.setVisibility(View.GONE);
            } else {
                couponNum = result.getModel().getCoupons().size();
                orderBikeCouponRl.setVisibility(View.VISIBLE);
                orderBikeCouponBlock.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        progressBarBikeSettlement.setVisibility(View.GONE);
    }

    @OnClick({R.id.order_bike_yh_rl, R.id.order_bike_return, R.id.order_bike_ll_haoshi, R.id.order_bike_coupon_rl, R.id.order_rule})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_bike_return:
                finish();
                break;
            case R.id.order_bike_coupon_rl:
                Intent intent1 = new Intent(mContext, ChooseCouponActivity.class);
                intent1.putExtra(IntentKey.INTENT_TRIP_MODE, "2");
                intent1.putExtra(IntentKey.INTENT_BIKE_ID, bikeId);
                intent1.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                startActivityForResult(intent1, Constants.REQUEST_CHOOSE_COUPON);
                break;
            case R.id.order_rule:
                Intent intent2 = new Intent();
                intent2.setClass(mContext, WebViewActivity.class);
                intent2.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, "计价规则");
                intent2.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlConstant.Request.VALUE_RULES);
                startActivity(intent2);
                break;
        }
    }

}
